﻿# coding:utf-8
import MySQLdb
import ConfigParser

class HtmlOutputer(object):
	def __init__(self, filename):
		self.f = open(filename, 'w')
		
	def output_detail_url(self,detail_urls):
		for url in detail_urls:
			self.f.write(url + '\n')
			self.f.flush()
	
	def close_file(self):
		self.f.close()
	

class DbOutputer(object):
	def __init__(self):
		self.cfg = ConfigParser.ConfigParser()
		self.conn = None
		self.cursor = None
	
	def load(self):
		self.cfg.read('config.ini')
		v_host = self.cfg.get('mysql','host')
		v_port = self.cfg.get('mysql','port')
		v_user = self.cfg.get('mysql','user')
		v_passwd = self.cfg.get('mysql','passwd')
		v_db = self.cfg.get('mysql','db')
		self.conn = MySQLdb.Connect(host=v_host,port=int(v_port),user=v_user,passwd=v_passwd,db=v_db,charset='utf8')
		
	
	def output(self, d):
		self.cursor = self.conn.cursor()
		sql = "INSERT wandoujia(app_pkg,app_category,app_name,app_size,category_list,app_version,app_system,app_source,app_time,app_url,craw_time) VALUES('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s',NOW())" %(d.get('app_pkg'),d.get('app_category'),d.get('app_name'),d.get('app_size'),d.get('category_list'),d.get('app_version'),d.get('app_system'),d.get('app_source'),d.get('app_time'),d.get('app_url'))
		#print 'sql--%s' %sql
		self.cursor.execute(sql)
		self.conn.commit()
		
	def close(self):
		if self.conn is not None:
			self.conn.close()
		if self.cursor is not None:
			self.cursor.close()

class ErrorLogOutput(object):
	def __init__(self,file):
		self.f = open(file,'a')
	
	def output(self,data):
		self.f.write(data + '\n')
		self.f.flush()
	
	def close(self):
		self.f.close()